<template>
  <div>
    <el-select
      style="width: 30%"
      placeholder="请输入标签名称"
      v-model="searchValue"
      default-first-option="true"
      filterable
      clearable
    >
      <el-option
        v-for="item in tableData"
        :key="item.id"
        :label="item.categoryName"
        :value="item.categoryName"
      >
      </el-option>
    </el-select>
    <el-button @click="searchButton">搜索</el-button>
    <el-button style="float: right" @click="[(dialogFormVisible = true), (isEdit = false)]"
      >添加分类</el-button
    >
  </div>
  <el-table :data="currentPageData" style="width: 100%">
    <el-table-column fixed prop="categoryName" label="分类名称" />
    <el-table-column prop="description" label="分类描述" />
    <el-table-column fixed="right" label="操作">
      <template #default="scope">
        <el-button link type="primary" size="small" @click="handleClick(scope.row)">
          编辑
        </el-button>
        <el-popconfirm
          title="确定删除该标签吗？"
          @confirm="deleteCategory(scope.row.id)"
          confirm-button-text="确认"
          cancel-button-text="取消"
        >
          <template #reference>
            <el-button link type="primary" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :page-sizes="[5, 10, 20, 50, 100]"
    layout="total,sizes, prev, pager, next"
    :total="total"
    style="float: right"
  >
  </el-pagination>
  <el-dialog v-model="dialogFormVisible" title="添加分类" width="500">
    <el-form :model="form">
      <el-form-item label="标签名称" :label-width="0">
        <el-input v-model="form.categoryName" placeholder="请输入标签名称" autocomplete="off" />
      </el-form-item>
      <el-form-item label="标签描述" :label-width="0">
        <el-input
          v-model="form.description"
          placeholder="请输入标签描述"
          autocomplete="off"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogSure"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import myAxios from '../../axios/myAxios'
import { onMounted } from 'vue'
import { ElMessage } from 'element-plus'
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return tableData.value.slice(start, end)
})
const handleCurrentChange = (newPage: number) => {
  currentPage.value = newPage
}
const handleSizeChange = (newSize: number) => {
  pageSize.value = newSize
  currentPage.value = 1
}

const form = reactive({
  categoryName: '',
  description: '',
})
const isEdit = ref(false)
const idForEdit = ref(0)
const dialogFormVisible = ref(false)
const dialogSure = () => {
  if (isEdit.value) {
    myAxios
      .put('/category/update', {
        id: idForEdit.value,
        categoryName: form.categoryName,
        description: form.description,
      })
      .then((res) => {
        if (res.data.code === 200) {
          ElMessage({
            message: '修改成功',
            type: 'success',
          })
          init()
        } else {
          ElMessage({
            message: '修改失败',
            type: 'error',
          })
        }
      })
  } else {
    myAxios
      .post('/category/create', {
        categoryName: form.categoryName,
        description: form.description,
      })
      .then((res) => {
        if (res.data.code === 200) {
          ElMessage({
            message: '添加成功',
            type: 'success',
          })
          init()
        } else {
          ElMessage({
            message: '添加失败',
            type: 'error',
          })
        }
      })
  }

  form.categoryName = ''
  form.description = ''
  dialogFormVisible.value = false
}
const handleClick = (row) => {
  idForEdit.value = row.id
  form.categoryName = row.categoryName
  form.description = row.description
  isEdit.value = true
  dialogFormVisible.value = true
}

const deleteCategory = (id: number) => {
  myAxios.delete(`/category/delete/${id}`).then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: '删除成功',
        type: 'success',
      })
      init()
    } else {
      ElMessage({
        message: '删除失败',
        type: 'error',
      })
    }
  })
}

const tableData = ref([])
const init = () => {
  myAxios.get('/category/all').then((res) => {
    if (res.data.code === 200) {
      tableData.value = res.data.data
      total.value = tableData.value.length
    }
  })
}

const searchValue = ref('')
const searchButton = () => {
  if (!searchValue.value) {
    init()
  } else {
    myAxios.get(`/category/name/${searchValue.value}`).then((res) => {
      if (res.data.code === 200) {
        const data = [
          {
            categoryName: res.data.data.categoryName,
            description: res.data.data.description,
          },
        ]
        tableData.value = data
        total.value = tableData.value.length
      }
    })
  }
}
onMounted(() => {
  init()
})
</script>
